package tbs.framework.boardcast.subject.impl;

import tbs.framework.boardcast.notify.INotifying;
import tbs.framework.boardcast.obsever.IObserver;
import tbs.framework.boardcast.subject.ISubject;

import java.util.LinkedList;
import java.util.List;

/**
 * 简单的本地发布者 实现
 *
 * @author tongj
 */
public class SimpleSubjectImpl implements ISubject {

    List<IObserver> observers = new LinkedList<>();

    private Long id = 0L;

    public SimpleSubjectImpl() {
    }

    public SimpleSubjectImpl(Long id, List<IObserver> observers) {
        this.observers = observers;
        this.id = id;
    }

    public SimpleSubjectImpl(Long id) {
        this.id = id;
    }
    @Override
    public Long getSubjectId() {
        return id;
    }

    @Override
    public void addObserver(IObserver observer) {
        observers.add(observer);
    }

    @Override
    public void removeObserver(IObserver observer) {
        observers.remove(observer);
    }

    @Override
    public int getObserverCount() {
        return observers.size();
    }

    @Override
    public void notifyObservers(INotifying notifying) {
        for (IObserver observer : observers) {
            observer.handle(notifying);
        }
    }
}
